import { 
    selLv1ByUserId, 
    addTelBookGroup, 
    delTelBookGroup, 
    updateTelBookGroup, 
    selTelBookByUserId,
    addTelBook,
    updateTelBook,
    delTelBook
} from '@/services/telBook';
import { errNotification, successNotification } from '@/utils/notification';

export default {

    namespace: 'telBook',
    
    state: {
        telBookList: [], //所有分组节点树形集合
        telList: [], //通讯录数组
        selName: '', //查询姓名条件
        selPhone: '', //查询电话条件
        page: 1,
        total: 0,
        addShow: false, //新增根分组输入框
        selectedNode:{ //选中的分组节点信息
            id: false,
            name: false
        },
        upAddInfo:{ //修改添加对话框信息
            title: "", //标题
            visible: false, //可见
            action: false, //要提交的action 新增或更新
            isUpdate: false, //是否更新了表单数据
            //表单数据
            id: null, 
            groupId: null,
            name: null,
            sex: null,
            duties: null,
            phone: null,
            landTel: null,
            cliqueTel: null
        }
    },
    
    effects: {
        *fetchSelLv1ByUserId(param, { call, put }) {
            const response = yield call(selLv1ByUserId, param);
            if(response.success){
                yield put({
                    type: 'selLv1ByUserId',
                    payload: response.data,
                });
            }
        },
        *fetchAddTelBookGroup(param, { call, put }) {
            const res1 = yield call(addTelBookGroup, param)
            if(res1.success){
                successNotification("分组添加成功","")
                const response = yield call(selLv1ByUserId, param);
                if(response.success){
                    yield put({
                        type: 'selLv1ByUserId',
                        payload: response.data,
                    });
                }
            }else{
                errNotification("分组添加失败","")
            }
            yield put({
                type: 'addTelGroupInpShow',
                payload: {addShow: false},
            });
        },
        *fetchDelTelBookGroup(param, { call, put }) {
            const res = yield call(delTelBookGroup, param);
            if(res.success){
                successNotification("分组删除成功","")
                const response = yield call(selLv1ByUserId, param);
                if(response.success){
                    yield put({
                        type: 'selLv1ByUserId',
                        payload: response.data,
                    });
                }
            }else{
                errNotification("分组删除失败","")
            }
        },
        *fetchUpdateTelBookGroup(param, { call, put }) {
            const res = yield call(updateTelBookGroup, param);
            if(res.success){
                successNotification("分组修改成功","")
                const response = yield call(selLv1ByUserId, param);
                if(response.success){
                    yield put({
                        type: 'selLv1ByUserId',
                        payload: response.data,
                    });
                }
            }else{
                errNotification("分组修改失败","")
            }
        },
        *fetchSelTelBookByUserId(param, { call, put }) {
            const response = yield call(selTelBookByUserId, param);
            if(response.success){
                yield put({
                    type: 'selTelBookByUserId',
                    payload: response.data,
                });
            }
        },
        *fetchAddTelBook(param, { call, put }) {
            const response = yield call(addTelBook, param);
            param.type = null;
            param.upAddInfo = null;
            if(response.success){
                successNotification("通讯簿添加成功","")
                const response2 = yield call(selTelBookByUserId, param);
                if(response2.success){
                    yield put({
                        type: 'selTelBookByUserId',
                        payload: response2.data,
                    });
                    yield put({
                        type: 'setUpAddInfo',
                        payload: {
                            title: "",
                            visible: false,
                            action: "",
                            isUpdate: false,
                            groupId: null,
                            id: null,
                            name: null,
                            sex: null,
                            duties: null,
                            phone: null,
                            landTel: null,
                            cliqueTel: null
                        },
                    });
                }
            }else{
                errNotification("通讯簿添加失败","")
            }
        },
        *fetchUpdateTelBook(param, { call, put }) {
            const response = yield call(updateTelBook, param);
            param.type = null;
            param.upAddInfo = null;
            if(response.success){
                successNotification("通讯簿修改成功","")
                const response2 = yield call(selTelBookByUserId, param);
                if(response2.success){
                    yield put({
                        type: 'selTelBookByUserId',
                        payload: response2.data,
                    });
                    yield put({
                        type: 'setUpAddInfo',
                        payload: {
                            title: "",
                            visible: false,
                            action: "",
                            isUpdate: false,
                            groupId: null,
                            id: null,
                            name: null,
                            sex: null,
                            duties: null,
                            phone: null,
                            landTel: null,
                            cliqueTel: null
                        },
                    });
                }
            }else{
                errNotification("通讯簿修改失败","")
            }
        },
        *fetchUpdateTelBookForGroup(param, { call, put }) {
            const response = yield call(updateTelBook, param);
            if(response.success){
                successNotification("通讯簿修改分组成功","")
                console.log(param)
                const response2 = yield call(selTelBookByUserId, param);
                if(response2.success){
                    yield put({
                        type: 'selTelBookByUserId',
                        payload: response2.data,
                    });
                }
            }else{
                errNotification("通讯簿修改分组失败","")
            }
        },
        *fetchDelTelBook(param, { call, put }) {
            const response = yield call(delTelBook, param);
            param.type = null;
            if(response.success){
                successNotification("通讯簿删除成功","")
                const response2 = yield call(selTelBookByUserId, param);
                if(response2.success){
                    yield put({
                        type: 'selTelBookByUserId',
                        payload: response2.data,
                    });
                }
            }else{
                errNotification("通讯簿删除失败","")
            }
        },
    },
    
    reducers: {
        selLv1ByUserId(state, action) {
            return {
                ...state,
                telBookList: action.payload,            
            };
        },
        addTelGroupInpShow(state, action) {
            return {
                ...state,
                addShow: action.payload.addShow           
            };
        },
        setSelectedNode(state, action) {
            return {
                ...state,
                selectedNode: action.payload           
            };
        },
        setUpAddInfo(state, action) {
            return {
                ...state,
                upAddInfo: action.payload           
            };
        },
        selTelBookByUserId(state, action) {
            if(action.payload.list){
                action.payload.list.map((item,index)=>{
                    item.key = index
                })
            }
            return {
                ...state,
                telList: action.payload.list,
                page: action.payload.pageNum,
                total: action.payload.total            
            };
        },
        setSelName(state, action) {
            console.log("=============="+action.payload)
            return {
                ...state,
                selName: action.payload           
            };
        },
        setSelPhone(state, action) {
            return {
                ...state,
                selPhone: action.payload           
            };
        },
    },

}